﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SzCourt.Logic.BLL;
using KMFrame.Util;
using Newtonsoft.Json;
using SzCourt.Logic.NBL.DBEntities;
using Core.IO;


namespace SzCourt.WebSite.lian
{
    public partial class application : System.Web.UI.Page
    {
        public string _FirstList = string.Empty;
        public string _SecondList = string.Empty;
        public string _ThirdList = string.Empty;
        public string _FourthList = string.Empty;

        private int fileMaxSize = 1024 * 1024 * 10;
        private string _AppContent = string.Empty;
        private string _PreCaseInfo = string.Empty;
        private string _CauseOfActionID = string.Empty;
        private string _CaseFiles = string.Empty;
        pPreCaseInfo mPreCaseInfo = new pPreCaseInfo();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                getParam();
                GetCauseOfActionList();
            }
        }

        //初始化参数
        private void getParam()
        {
            this.hdnPreCaseType.Value = string.IsNullOrEmpty(Request.QueryString["t"]) ? "1" : Request.QueryString["t"];

            //this._AppContent = string.IsNullOrEmpty(Request.Form["AppContent"]) ? "" : Request.Form["AppContent"];


            //this._CauseOfActionID = string.IsNullOrEmpty(Request.Form["hdnCauseOfActionID"]) ? "1" : Request.Form["hdnCauseOfActionID"];
            //this._PreCaseInfo = string.IsNullOrEmpty(Request.Form["PreCaseInfo"]) ? "" : Request.Form["PreCaseInfo"];
            //this._CaseFiles = string.IsNullOrEmpty(Request.Form["PreCaseFile"]) ? "" : Request.Form["PreCaseFile"];
            //if (!string.IsNullOrEmpty(this._PreCaseInfo))
            //{
            //    string[] aCaseInfo = this._PreCaseInfo.Split('~');
            //    mPreCaseInfo.QueryPWD = aCaseInfo[0];
            //    mPreCaseInfo.Email = aCaseInfo[1];
            //    mPreCaseInfo.Phone = aCaseInfo[2];
            //    mPreCaseInfo.PreCaseType = short.Parse(aCaseInfo[3]);
            //    mPreCaseInfo.CauseOfActionID = int.Parse(this._CauseOfActionID);
            //}
        }

        private void GetCauseOfActionList()
        {
             PreCase bll = new PreCase();
            CauseOfActionList result = (CauseOfActionList)bll.GetCauseOfActionList();
            
            if (result != null)
            {
                this.sltFirstList.DataSource = result.FirstList;
                this.sltFirstList.DataTextField = "CauseOfAction";
                this.sltFirstList.DataValueField = "CauseOfActionID";
                this.sltFirstList.DataBind();

                _FirstList = JavaScriptConvert.SerializeObject(result.FirstList);
                _SecondList = JavaScriptConvert.SerializeObject(result.SecondList);
                _ThirdList = JavaScriptConvert.SerializeObject(result.ThirdList);
                _FourthList = JavaScriptConvert.SerializeObject(result.FourthList);
            }
        }


        //获取参数值
        private void getApplicationParam()
        {
            this._AppContent = this.AppContent.Value;

            this._CauseOfActionID = this.hdnCauseOfActionID.Value;
            this._PreCaseInfo = this.PreCaseInfo.Value;
            this._CaseFiles = this.uploadfile.Value;
            string[] aCaseInfo = this._PreCaseInfo.Split('~');
            mPreCaseInfo.QueryPWD = aCaseInfo[0];
            mPreCaseInfo.Email = aCaseInfo[1];
            mPreCaseInfo.Phone = aCaseInfo[2];
            mPreCaseInfo.PreCaseType = short.Parse(aCaseInfo[3]);
            if(!string.IsNullOrEmpty(aCaseInfo[4]))
                mPreCaseInfo.StartingMark = decimal.Parse(aCaseInfo[4]);
            mPreCaseInfo.CauseOfActionID = int.Parse(this._CauseOfActionID);
            
        }

        private void createrInfo()
        {
            PreCassCollection param = new PreCassCollection();

            List<pPreParties> mPartiesList = new List<pPreParties>();
            string[] aApps = this._AppContent.Split('$');//切分为原被告
            for (int a = 0; a < aApps.Length; a++)
            {
                string[] alis = aApps[a].Split('~');//切分有多少个原被告
                for (int l = 1; l < alis.Length; l++)
                {
                    string[] aentity = alis[l].Split('&');//切分出每一个元素
                    pPreParties tmpPP = new pPreParties();
                    tmpPP.PartiesType = short.Parse(aentity[0]);
                    tmpPP.PartiesClass = short.Parse(aentity[1]);
                    tmpPP.PartiesName = aentity[2];

                    switch (aentity[1])
                    {
                        case "1":
                            tmpPP.IdentityCard = aentity[3];
                            tmpPP.Birth = aentity[4];
                            tmpPP.Nationaly = aentity[5];
                            tmpPP.National = aentity[6];
                            tmpPP.Political = aentity[7];
                            tmpPP.Degree = aentity[8];
                            tmpPP.Phone1 = aentity[9];
                            tmpPP.PostalCode = aentity[10];
                            tmpPP.Domicile = aentity[11];
                            tmpPP.Address = aentity[12];
                            break;

                        case "2":
                            tmpPP.LegalPerson = aentity[3];
                            tmpPP.Phone1 = aentity[4];
                            tmpPP.PostalCode = aentity[5];
                            tmpPP.Domicile = aentity[6];
                            tmpPP.Address = aentity[7];
                            break;
                        case "3":
                            tmpPP.LegalPerson = aentity[3];
                            tmpPP.Phone1 = aentity[4];
                            tmpPP.PostalCode = aentity[5];
                            tmpPP.Domicile = aentity[6];
                            tmpPP.Address = aentity[7];
                            break;
                    }
                    mPartiesList.Add(tmpPP);
                }
            }

            List<pPreCaseFile> mFileList = new List<pPreCaseFile>();
            //string[] mfiles = this._AppContent.Split('~');//切分为每个文件
            //for (int a = 0; a < mfiles.Length; a++)
            {
                pPreCaseFile casefile = new pPreCaseFile();
                casefile.FileName = this._AppContent;
                mFileList.Add(casefile);

            }
            //当事人列表
            param.PrePartiesList = mPartiesList;

            //初始化基本信息
            param.PreCaseInfo = mPreCaseInfo;

            //文件列表
            param.PreCaseFile = mFileList;

            PreCase bll = new PreCase();
            string result = (string)bll.CreatePreCase(param);
            doUpload();
            if (result.Length > 0)
            {
                Response.Redirect("/lian/chengong.aspx?suc=1&num=" + result + "&pwd=" + mPreCaseInfo.QueryPWD);
            }
            else
            {
                Response.Redirect("/lian/chengong.aspx?suc=2");
            }
        }

        protected void doUpload()
        {
            try
            {

                //获取上传的文件

                //HttpPostedFile file = (HttpPostedFile)Request.Files["fileUpload"];

                //判断文件大小
                //if (file.ContentLength > this.fileMaxSize)//file.FileSize > this.fileMaxSize
                //{
                //    throw new Exception("文件超过允许的大小！");
                //}
                //string tempDir = "/file/yulian/";
                //string urlPath = tempDir + file.FileName;

                //if (!Directory.Exists(Server.MapPath(tempDir)))
                //{
                //    Directory.CreateDirectory(Server.MapPath(tempDir));
                //}
                //file.SaveAs(Server.MapPath(urlPath));

                if (this.FileUpload1.HasFile)
                {
                    if (this.FileUpload1.PostedFile.ContentLength > this.fileMaxSize)//file.FileSize > this.fileMaxSize
                    {
                        throw new Exception("文件超过允许的大小！");
                    }
                    string tempDir = @"/file/yulian";
                    string urlPath = tempDir +"/"+ this.FileUpload1.FileName;

                    //if (!Directory.Exists(Server.MapPath(tempDir)))
                    //{
                    //    Directory.CreateDirectory(Server.MapPath(tempDir));
                    //}
                    //上传文件并指定上传目录的路径   
                    this.FileUpload1.PostedFile.SaveAs(Server.MapPath(urlPath));   
 
                }

            }
            catch (Exception ex)
            {
                //WriteJs("objUploadFile.uploaderror('" + ex.Message + "');");
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            getApplicationParam();
            createrInfo();
        }
    }
}